# -*- coding: utf-8 -*-
# %%
import numpy as np
import xlrd
import time
import collections
import os
import sys
from matplotlib import pyplot as plt

sys.path.append(r'X:\Work\Rb87Python')
sys.path.append(r'D:\vc\Rb87Python')

import socket

os.system('chcp 65001')
Host_Name = socket.gethostname()
if Host_Name == 'VictorChengPC':
    Computer_Name = 'X:\\Work\\mail.ustc.edu.cn\\Rb87BEC - Files\\'
elif Host_Name == 'VictorChengXPS':
    Computer_Name = 'X:\\Work\\mail.ustc.edu.cn\\Rb87BEC - Files\\'
elif Host_Name == 'calcenter2':
    Computer_Name = 'D:\\vc\\mail.ustc.edu.cn\\Rb87BEC - Files\\'

# %%
time_start = time.time()
time_start_struct = time.localtime(time_start)

# %%
Fit_Prefix = ['NoFit', '']
Fit_BEC_Switch = 0
Data_Cut_Shape_list = ['Rectangle', 'Rectangle']
Data_Cut_Shape = 'RectangleRectangle'
Filter_Size = 2

Data_Table = xlrd.open_workbook(Computer_Name + r'Rb87files\3DSOC_Quench\Quench.xlsx').sheets()[0]
TimeJoin_Table = xlrd.open_workbook(Computer_Name + r'Rb87files\3DSOC_Quench\Quench.xlsx').sheets()[1]
Meta_Table = xlrd.open_workbook(Computer_Name + r'Rb87files\3DSOC_Quench\Quench.xlsx').sheets()[2]
TimeJoin_FigureFolder = 'D:\\Rb87figures\\3DSOC_Quench\\figuresNoFit\\TimeJoin_Pic\\'
if Host_Name == 'calcenter2':
    os.system('mkdir ' + TimeJoin_FigureFolder)
TimeJoin_DataFolder = Computer_Name + 'Rb87Data\\3DSOC_Quench\\NoFit\\TimeJoin\\'
os.system('mkdir ' + '\"' + TimeJoin_DataFolder + '\"')
# %%
TimeJoinRow_List = range(42, 42 + 1)
# TimeJoinRow_List = [2, 8, 9, 10]

for TimeJoinRow_Index in range(len(TimeJoinRow_List)):
    TimeJoinRow = TimeJoinRow_List[TimeJoinRow_Index]
    TimeJoinData = TimeJoin_Table.row_values(TimeJoinRow - 1)
    Detuning = TimeJoinData[2]
    Mat_Data = []
    Mat_Data_command = TimeJoinData[3]
    exec(Mat_Data_command)
    SP_TimeJoin_1d = collections.OrderedDict()
    SP_rened_TimeJoin_1d = collections.OrderedDict()
    ts_TimeJoin = []
    Bad_Pics_TimeJoin = []
    for Line_Index in range(len(Mat_Data)):
        Line = Mat_Data[Line_Index]
        Input_Matrix = Data_Table.row_values(Line - 1)
        YearMonthDay = str(int(Input_Matrix[0]))
        DataFolder_Name = Computer_Name + 'Rb87Data\\3DSOC_Quench\\' + Fit_Prefix[
            Fit_BEC_Switch] + '\\' + YearMonthDay + '\\All_Spin_Polarization\\'
        Ini_Photo = int(Input_Matrix[1])
        Fin_Photo = int(Input_Matrix[2])
        V0 = Input_Matrix[4]
        Vy = Input_Matrix[5]
        Omega = Input_Matrix[6]
        Detuning = Input_Matrix[10]
        Cam = Input_Matrix[8]
        Time_Start = int(Input_Matrix[11])
        Time_Interval = int(Input_Matrix[12])
        Time_End = int(Input_Matrix[13])
        Photo = str(Ini_Photo) + 'To' + str(Fin_Photo)
        ts = np.arange(Time_Start, Time_End + Time_Interval, Time_Interval)
        if Cam == '22834':
            Data = np.load(DataFolder_Name +
                           'Cam_' + Cam + '_' + Photo + r'Spin_Polarization_FBZ' +
                           Data_Cut_Shape + '_1d.npz', allow_pickle=True)
        else:
            Data = np.load(DataFolder_Name +
                           'Cam_' + Cam + '_Line=%d' % Line + '_' + Photo + r'Spin_Polarization_FBZ' +
                           Data_Cut_Shape + '_1d.npz')
        SPs_1d = Data['SPs_1d'].item()
        Bad_Pics = Data['Bad_Pics']
        Bad_Pics_TimeJoin = np.concatenate((Bad_Pics_TimeJoin, Bad_Pics))
        SP_TimeJoin_1d.update(SPs_1d)  # = {**SP_TimeJoin, **SPs}
        if 'SPs_rened_1d' in Data:
            SPs_rened_1d = Data['SPs_rened_1d'].item()
            SP_rened_TimeJoin_1d.update(SPs_rened_1d)
        else:
            SP_rened_TimeJoin_1d.update({0: 0})
        ts_TimeJoin = np.concatenate((ts_TimeJoin, ts))
    np.savez(TimeJoin_DataFolder + 'TimeJoinRow=%d_1d.npz' % TimeJoinRow, SP_TimeJoin_1d=SP_TimeJoin_1d,
             ts_TimeJoin=ts_TimeJoin, SP_rened_TimeJoin_1d=SP_rened_TimeJoin_1d)
